/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * Description: This activity displays a search box and a list of library locations. Searching or clicking on a location
 * 				launches the EventListActivity with the appropriate list of events.
 */

package us.in.lib.acpl.Mobile_ACPL;

import com.google.analytics.tracking.android.EasyTracker;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import us.in.lib.acpl.Mobile_ACPL.adapters.EventMainAdapter;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseListActivity;

public class EventMainActivity extends BaseListActivity implements
		OnItemClickListener, OnClickListener, OnKeyListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.getActionBar().setTitle("  Events");
		super.onCreate(savedInstanceState);

		setContentView(R.layout.event_main);
		setListAdapter(new EventMainAdapter(this, this.getResources()
				.getStringArray(R.array.libraryLocationsEventListDisplay)));
		this.getListView().setOnItemClickListener(this);
		this.findViewById(R.id.eventSearchButton).setOnClickListener(this);
		this.findViewById(R.id.eventSearch).setOnKeyListener(this);

	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }

	public void search() {

		this.findViewById(R.id.eventSearchButton).setVisibility(View.GONE);
		this.findViewById(R.id.eventSearchBar).setVisibility(View.VISIBLE);

		EditText et = (EditText) this.findViewById(R.id.eventSearch);
		String term = et.getText().toString();

		Intent intent = new Intent(this, EventListActivity.class);
		intent.setAction("search");
		intent.putExtra("term", term);
		startActivity(intent);
	}

	@Override
	public void onClick(View view) {

		search();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

		Intent intent = new Intent(this, EventListActivity.class);
		intent.setAction("list");
		String[] locs = this.getResources().getStringArray(
				R.array.libraryLocationsEventListDisplay);
		intent.putExtra("library", locs[pos]);
		startActivity(intent);

	}

	@Override
	public boolean onKey(View view, int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_ENTER
				&& event.getAction() == KeyEvent.ACTION_UP) {
			search();
		}
		return false;
	}

	@Override
	public void onResume() {
		super.onResume();
		this.findViewById(R.id.eventSearchButton).setVisibility(View.VISIBLE);
		this.findViewById(R.id.eventSearchBar).setVisibility(View.GONE);

	}
	

}
